{{extend "base.html"}}

{{block head}}
{{use "easyui_extension",datagrid_detailview=True}}
<script type="text/javascript" charset="utf-8">
	var datagrid;
	var tree;
	var pstyle;
	$(function() {
				
			$('#btn-search,#btn-search-cancel').linkbutton();
			searchWin = $('#search-fuzzy').window({
				closed: true,
				collapsible:false,
				minimizable:false,
				resizable: false,
				maximized:true, //初始最大化
				modal: true
			});
			searchForm = searchWin.find('form');
		
		tree = $('#cr_tree').tree({
				//checkbox: false,
				url: '/idcpost/computerroomtree?pid=0',
				onBeforeExpand: function (node, param) {
					//alert(node.id);
					$('#cr_tree').tree('options').url = "/idcpost/computerroomtree?pid=" + node.id; // change the url                       
					//param.myattr = 'test';    // or change request parameter
				},
				onClick: function (node) {
					clickTree(node.id);
				}
		});
		
		datagrid = $('#datagrid').datagrid({
			url : '/devicespost/server',
			title : '',
			iconCls : 'icon-save',
			//method:'get',
			pagination : true,
			pagePosition : 'bottom',
			pageSize : 20,
			pageList : [ 20, 30, 40 ],

			idField : 'id',
			//checkOnSelect : false,
			//selectOnCheck : true,
			//左右滚动条

			columns : [ [{
				title : '编号',
				field : 'id',
				width : 150,
				sortable : true,
				checkbox : true
				}, {
				title : '资产编号',
				field : 'server_asset_tag',
				width : 80,
				hidden:true
				}, {
				title : '机房',
				field : 'computerroom_id',
				width : 100,
				sortable : true
				}, {
				title : '机柜编号',
				field : 'rackspace_id',
				width : 80
			}, {
				title : '品牌',
				field : 'server_manufacturer',
				width : 50
			}, {
				title : '型号',
				field : 'server_type',
				width : 50
			}, {
				title : '序列号',
				field : 'server_sn',
				width : 80
			},{
				title : '是否虚拟',
				field : 'is_virtual',
				width : 100,
				hidden: true,
				styler:cellStylerhidden
			}, {
				title : '主IP地址',
				field : 'ipaddr',
				width : 100,
				styler:cellStyler
			}, {
				title : '设备状态',
				field : 'hardwarestatus_id',
				width : 60,
				hidden: true
			}, {
				title : '部门',
				field : 'department_name',
				width : 130
			}, {
				title : '项目',
				field : 'opersgroupproject_name',
				width : 100
			}, {
				title : '应用主题',
				field : 'applicationtype_name',
				width : 100
			}, {
				title : '备注',
				field : 'comment',
				width : 150
			},{
				title : '操作',
				field : 'content',
				width : 100,
				formatter : function(value, rowData, rowIndex) {
					return '<span class="icon-search" style="display:inline-block;vertical-align:middle;width:16px;height:16px;"></span><a href="javascript:void(0);" onclick="showcontent(' + rowIndex + ');">查看详细</a>';
				}
			}] ],
			toolbar : '#toolbar',
			rowStyler: function(index,row){
				//0表示关机，行颜色为红色
	            if (row.hardwarestatus_id == '0'){
	                return 'background-color:#FF0000;color:#fff;font-weight:bold;';
	            }
	        },
			view: detailview,
			detailFormatter:function(index,row){
				return '<div class="ddv" style="padding:5px 0"></div>';
			},
			onExpandRow: function(index,row){
				var ddv = $(this).datagrid('getRowDetail',index).find('div.ddv');
				ddv.panel({
					height:80,
					border:false,
					cache:false,
					href:'/devices/server_getdetail?id='+row.id,
					onLoad:function(){
						$('#datagrid').datagrid('fixDetailRowHeight',index);
					}
				});
				$('#datagrid').datagrid('fixDetailRowHeight',index);
			}

		});
		

	});
	

	//模糊搜索
	function Openfuzzy() {
		datagrid.datagrid('clearSelections');
		datagrid.datagrid('unselectAll');
		searchWin.window('open');
		searchForm.form('clear');
	}
	
	function clickTree(nodeid) {
		datagrid.datagrid({ url: '/devicespost/server?action=list&pid=' + nodeid });
		datagrid.datagrid('clearSelections');
	}
	function precise_ok() {
		datagrid.datagrid('clearSelections');
		datagrid.datagrid('unselectAll');
		var search_value = $("#search_value").val();
		var search_type = $('#search_type').combobox('getValue');

		if (search_type == 'ethernet_ip_fuzzy'){
			//IP模糊搜索
			datagrid.datagrid({ url: '/devicessearch/server?action=ipfuzzy', 
				queryParams: {search_type: search_type,
					search_value:search_value
				} 
			});
		}
		else{
			datagrid.datagrid({ url: '/devicessearch/server?action=precise', 
							queryParams: {search_type: search_type,
											search_value:search_value
										} 
						});
		}
	}
	function SearchOK() {
		var cr_name = $("#cr_name").val();
		var rackspace_name = $("#rackspace_name").val();
		var server_manufacturer = $("#server_manufacturer").val();
		var server_type = $("#server_type").val();
		var serverdisk_num = $("#serverdisk_num").val();
		var serverdisk_size = $("#serverdisk_size").val();
		var serverdisk_speed = $("#serverdisk_speed").val();
		var servercpu_type = $("#servercpu_type").val();
		var servercpu_hz = $("#servercpu_hz").val();
		var servercpu_num = $("#servercpu_num").val();
		var servermem_type = $("#servermem_type").val();
		var servermem_hz = $("#servermem_hz").val();
		var servermem_size = $("#servermem_size").val();
		var servermem_num = $("#servermem_num").val();
		var servermemall_size = $("#servermemall_size").val();
		var r1 = $("#r1:checked").val();

		searchWin.window('close');
		datagrid.datagrid({ url: '/devicessearch/server?action=fuzzy', 
							queryParams: {cr_name: cr_name,
											rackspace_name:rackspace_name,
											server_manufacturer:server_manufacturer,
											server_type:server_type,
											serverdisk_num:serverdisk_num,
											serverdisk_size:serverdisk_size,
											serverdisk_speed:serverdisk_speed,
											servercpu_type:servercpu_type,
											servercpu_hz:servercpu_hz,
											servercpu_num:servercpu_num,
											servermem_type:servermem_type,
											servermem_hz:servermem_hz,
											servermem_size:servermem_size,
											servermem_num:servermem_num,
											servermemall_size:servermemall_size,
											r1:r1
							} });
	}
	
	function closeSearchWindow() {
		searchWin.window('close');
	}
	function edit() {
		var rows = datagrid.datagrid('getSelections');
		if (rows.length == 1) {
			if (rows[0].is_virtual == 1){
				var p = parent.sy.dialog({
					title : '编辑硬件信息',
					href : '/devices/serveredit?id=' + rows[0].id,
					width : 900,
					height : 500,
					buttons : [ {
						text : '编辑',
						handler : function() {
							var f = p.find('form');
							f.form('submit', {
								url : '/devices/serversave',
								success : function(d) {
									var json = $.parseJSON(d);
									if (json.success) {
										datagrid.datagrid('reload');
										p.dialog('close');
									}
									parent.sy.messagerShow({
										msg : json.msg,
										title : '提示'
									});
								}
							});
						}
					} ],
					onLoad : function() {
						var f = p.find('form');
						f.form('load', {
							id : rows[0].id,
							server_sn : rows[0].server_sn,
							server_asset_tag : rows[0].server_asset_tag,
							comment : rows[0].comment
						});
					}
				});
			}else{
				var p = parent.sy.dialog({
					title : '编辑硬件信息',
					href : '/devices/servereditvirtual?id=' + rows[0].id,
					width : 900,
					height : 500,
					buttons : [ {
						text : '编辑',
						handler : function() {
							var f = p.find('form');
							f.form('submit', {
								url : '/devices/serversavevirtual',
								success : function(d) {
									var json = $.parseJSON(d);
									if (json.success) {
										datagrid.datagrid('reload');
										p.dialog('close');
									}
									parent.sy.messagerShow({
										msg : json.msg,
										title : '提示'
									});
								}
							});
						}
					} ],
					onLoad : function() {
						var f = p.find('form');
						f.form('load', {
							id : rows[0].id,
							comment : rows[0].comment
						});
					}
				});
			}
		} else if (rows.length > 1) {
			parent.sy.messagerAlert('提示', '同一时间只能编辑一条记录！', 'error');
		} else {
			parent.sy.messagerAlert('提示', '请选择要编辑的记录！', 'error');
		}
	}
	function append() {
		var p = parent.sy.dialog({
			title : '添加设备信息',
			href : '/devices/serveradd',
			width : 900,
			height : 500,
			buttons : [ {
				text : '添加',
				handler : function() {
					var f = p.find('form');
					f.form('submit', {
						url : '/devices/serversave',
						success : function(d) {
							var json = $.parseJSON(d);
							if (json.success) {
								datagrid.datagrid('reload');
								p.dialog('close');
							}
							parent.sy.messagerShow({
								msg : json.msg,
								title : '提示'
							});
						}
					});
				}
			} ]
		});
	}
	
	function appendvirtual() {
		var p = parent.sy.dialog({
			title : '添加设备信息',
			href : '/devices/serveraddvirtual',
			width : 900,
			height : 500,
			buttons : [ {
				text : '添加',
				handler : function() {
					var f = p.find('form');
					f.form('submit', {
						url : '/devices/serversavevirtual',
						success : function(d) {
							var json = $.parseJSON(d);
							if (json.success) {
								datagrid.datagrid('reload');
								p.dialog('close');
							}
							parent.sy.messagerShow({
								msg : json.msg,
								title : '提示'
							});
						}
					});
				}
			} ]
		});
	}
	
	function del() {
		var rows = datagrid.datagrid('getChecked');
		var ids = [];
		if (rows.length > 0) {
			parent.sy.messagerConfirm('请确认', '您要删除当前所选项目？', function(r) {
				if (r) {
					for ( var i = 0; i < rows.length; i++) {
						ids.push(rows[i].id);
					}
					$.ajax({
						url : '/devices/serverdel',
						data : {
							id : ids.join(',')
						},
						dataType : 'json',
						success : function(d) {
							datagrid.datagrid('load');
							datagrid.datagrid('unselectAll');
							parent.sy.messagerShow({
								title : '提示',
								msg : d.msg
							});
						}
					});
				}
			});
		} else {
			parent.sy.messagerAlert('提示', '请勾选要删除的记录！', 'error');
		}
	}

	function undo() {
		datagrid.datagrid('clearSelections');
		datagrid.datagrid('unselectAll');
	}
	
	//查看明细
	function showcontent(rowIndex) {
		var rows = datagrid.datagrid('getRows');
		var row = rows[rowIndex];
		if (row.is_virtual == 1){
			var p = parent.sy.dialog({
				title : '设备明细 [' + row.title + ']',
				modal : true,
				maximizable : true,
				width : 900,
				height : 500,
				content : '<iframe src="/devices/serverdetail?id=' + row.id + '" frameborder="0" style="border:0;width:100%;height:99.4%;"></iframe>'
			});
		}else{
			var p = parent.sy.dialog({
				title : '设备明细 [' + row.title + ']',
				modal : true,
				maximizable : true,
				width : 900,
				height : 500,
				content : '<iframe src="/devices/serverdetailvirtual?id=' + row.id + '" frameborder="0" style="border:0;width:100%;height:99.4%;"></iframe>'
			});
		}

		datagrid.datagrid('unselectAll');
	}
	
	function exportExcel(){
		var node = tree.tree('getSelected');
		if (node == "" || node == null) {
			//导出所有
			window.location.href="/devices/exportExcel";
		}else{
			//导出选定节点
			window.location.href="/devices/exportExcel?id=" + node.id;
		}
	}


	function cellStylerhidden(value,row,index){
        if (value == 0){
            pstyle='background-color:#28FF28;color:#000000;';
        }else{
        	pstyle='';
        }
	}

	function cellStyler(value,row,index){
		//绿色表示虚拟机
        return pstyle;
	}
	
</script>
{{end}}

{{block bodyclass}}
<!--body class="easyui-layout" data-options="fit:true"-->
<body class="easyui-layout" style="overflow-y: hidden" scroll="no">
{{end}}

{{block content}}
	<div id="toolbar" style="display: none;">
		<table>
			<tr>
				<td><a href="javascript:void(0);" class="easyui-linkbutton" data-options="iconCls:'icon-add',plain:true" onclick="append();">添加物理机</a></td>
				<td><a href="javascript:void(0);" class="easyui-linkbutton" data-options="iconCls:'icon-add',plain:true" onclick="appendvirtual();">添加虚拟机</a></td>
				<td><a href="javascript:void(0);" class="easyui-linkbutton" data-options="iconCls:'icon-remove',plain:true" onclick="del();">删除</a></td>
				<td><a href="javascript:void(0);" class="easyui-linkbutton" data-options="iconCls:'icon-edit',plain:true" onclick="edit();">修改</a></td>
				<td><a href="javascript:void(0);" class="easyui-linkbutton" data-options="iconCls:'icon-undo',plain:true" onclick="undo();">取消选中</a></td>
				<td><div class="datagrid-btn-separator"></div></td>
				
				<td><a href="/devices/exportExcel" class="easyui-linkbutton" data-options="iconCls:'ext-icon-table_go',plain:true">导出全部</a></td>
				<td><a href="javascript:void(0);" class="easyui-linkbutton" data-options="iconCls:'ext-icon-table_go',plain:true" onclick="exportExcel();">导出</a></td>

			</tr>
		</table>
		<table>
			<tr>
				<td>
					<form id="searchpreciseForm">
						<table>
							<tr>
								<td><select class="easyui-combobox" id="search_type" name="search_type" style="width:80px;">
								<option value="ethernet_ip">IP地址</option>
								<option value="server_asset_tag">资产编号</option>
								<option value="system_hostname">主机名</option>
								<option value="server_sn">序列号</option>
								<option value="ethernet_ip_fuzzy">模糊IP地址</option>
								</select>
								</td>
								<td><input name="search_value" id="search_value" style="width: 150px;" /></td>
								<td><a href="javascript:void(0);" class="easyui-linkbutton" data-options="iconCls:'ext-icon-zoom',plain:true" onClick="precise_ok();">精确搜索</a><a href="javascript:void(0);" class="easyui-linkbutton" data-options="iconCls:'ext-icon-zoom_out',plain:true" onClick="$('#searchpreciseForm input').val('');datagrid.datagrid({ url: '/devicespost/server'});">清空</a></td>
							</tr>
						</table>
					</form>
				</td>
				<td><div class="datagrid-btn-separator"></div></td>
				<td><a href="javascript:void(0);" class="easyui-linkbutton" data-options="iconCls:'icon-search',plain:true" onclick="Openfuzzy();">模糊搜索</a></td>
			</tr>
		</table>
	</div>

    <div region="west" split="true" title="机房" data-options="collapsed:true" style="width: 180px;" id="west">
        <ul id="cr_tree">
        </ul>
    </div>
    <div region="center" style="width: 500px; height: 300px; padding: 1px; background: #eee;
        overflow-y: hidden">
        <div id="datagrid" fit="true">
        </div>
    </div>
    <div id="search-fuzzy" title="模糊查询" style="width: 350px; height: 200px;">
        <div style="padding: 5px 5px 20px 40px;">
            <form method="post">
            <table>
				<tr>
                    <td><input type="radio" id="r1" name="r1" value="or" />条件或</td>
                    <td><input type="radio" id="r1" name="r1" value="and"/>条件与</td>
                </tr>
                <tr>
                    <td>机房名称</td>
                    <td><input class="easyui-validatebox" name="cr_name" id="cr_name" style="width:150px" /></td>
                    <td>机柜</td>
                    <td><input class="easyui-validatebox" name="rackspace_name" id="rackspace_name" style="width:150px" /></td>
                </tr>
                <tr>
                    <td>品牌</td>
                    <td><input class="easyui-validatebox" name="server_manufacturer" id="server_manufacturer" style="width:150px" /></td>
                    <td>型号</td>
                    <td><input class="easyui-validatebox" name="server_type" id="server_type" style="width:150px" /></td>
                </tr>
				<tr>
                    <td>CPU型号</td>
                    <td><input class="easyui-validatebox" name="servercpu_type" id="servercpu_type" style="width:150px" /></td>
                    <td>CPU频率</td>
                    <td><input class="easyui-validatebox" name="servercpu_hz" id="servercpu_hz" style="width:150px" /></td>
                    <td>CPU数量</td>
                    <td><input class="easyui-validatebox" name="servercpu_num" id="servercpu_num" style="width:150px" /></td>
                </tr>
                <tr>
                    <td>硬盘数量</td>
                    <td><input class="easyui-validatebox" name="serverdisk_num" id="serverdisk_num" style="width:150px" /></td>
                    <td>硬盘大小</td>
                    <td><input class="easyui-validatebox" name="serverdisk_size" id="serverdisk_size" style="width:150px" /></td>
                    <td>硬盘转速</td>
                    <td><input class="easyui-validatebox" name="serverdisk_speed" id="serverdisk_speed" style="width:150px" /></td>
                </tr>
				<tr>
                    <td>内存型号</td>
                    <td><input class="easyui-validatebox" name="servermem_type" id="servermem_type" style="width:150px" /></td>
                    <td>内存频率</td>
                    <td><input class="easyui-validatebox" name="servermem_hz" id="servermem_hz" style="width:150px" /></td>
                    <td>单根内存容量</td>
                    <td><input class="easyui-validatebox" name="servermem_size" id="servermem_size" style="width:150px" /></td>
					<td>内存数量</td>
                    <td><input class="easyui-validatebox" name="servermem_num" id="servermem_num" style="width:150px" /></td>
					<td>总内存容量</td>
                    <td><input class="easyui-validatebox" name="servermemall_size" id="servermemall_size" style="width:150px" /></td>
				</tr>
            </table>
            </form>
        </div>
        <div style="text-align: center; padding: 5px;">
            <a href="javascript:void(0)" onClick="SearchOK()" id="btn-search" icon="icon-ok">确定</a>
            <a href="javascript:void(0)" onClick="closeSearchWindow()" id="btn-search-cancel" icon="icon-cancel">
                取消</a>
        </div>
    </div>
{{end}}